import 'package:flutter/material.dart';
import 'package:flutter_deer/widgets/custom_button.dart';

class DynamicButtonPage extends StatefulWidget {
  const DynamicButtonPage({Key? key}) : super(key: key);

  @override
  _DynamicButtonPageState createState() => _DynamicButtonPageState();
}

class _DynamicButtonPageState extends State<DynamicButtonPage> {
  final List<String> _buttonTexts = ['保存', '取消', '提交信息', '查看详情'];
  final List<Widget> _buttons = [];

  void _addButton() {
    if (_buttonTexts.length > _buttons.length) {
      setState(() {
        _buttons.add(
          CustomButton(
            text: _buttonTexts[_buttons.length],
            onPressed: () {
              ScaffoldMessenger.of(context).showSnackBar(
                SnackBar(content: Text('您点击了: ${_buttons.length + 1}')),
              );
            },
          ),
        );
      });
    } else {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('没有更多的按钮可以添加了')),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('动态按钮页面'),
      ),
      body: Column(
        children: [
          Expanded(
            child: Center(
              child: Text(
                _buttons.isEmpty ? '没有按钮' : '当前有 ${_buttons.length} 个按钮',
                style: const TextStyle(fontSize: 18),
              ),
            ),
          ),
          if (_buttons.isNotEmpty)
            Wrap(
              alignment: WrapAlignment.center,
              spacing: 8.0,
              runSpacing: 8.0,
              children: _buttons,
            ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _addButton,
        child: const Icon(Icons.add),
      ),
    );
  }
}
